package 力扣日常刷题.木22二月.第18天224;

import java.util.*;

/**
 * @author 帅小伙
 * @date 2022/2/25
 * @description
 * map转成entry进行排序
 */
public class Demo147字节跳动2017后端工程师实习生笔试题1 {
    public static void main(String[] args) {
        long[] weight = new long[10];
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        ArrayList<String>strings = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            String str = scanner.next();
            strings.add(str);
            char[] chars = str.toCharArray();
            for (int j = 0; j < chars.length; j++) {
                weight[chars[j] - 'A'] += Math.pow(10,chars.length - j - 1);
            }
        }
        Map<Integer, Long> map = new HashMap<>();

        for (int i = 0; i < 10; i++) {
            map.put(i,weight[i]);
        }
        ArrayList<Map.Entry<Integer, Long>> arrayList = new ArrayList(map.entrySet());
        Collections.sort(arrayList, (a,b)->a.getValue().compareTo(b.getValue()));
        int zero = findZero(arrayList, strings);
        Collections.sort(arrayList, (a,b)->b.getValue().compareTo(a.getValue()));

        int k = 10;
        long  ans = 0;
        for (Map.Entry<Integer, Long> entry : arrayList) {
            k--;
            if(entry.getKey() == zero) {
                k++;
            }else {
               ans += k * entry.getValue();
            }
        }
        System.out.println(ans);
    }

    private static int findZero(ArrayList<Map.Entry<Integer, Long>> map,ArrayList<String> strings) {
        int index = -1;
        for (Map.Entry<Integer, Long> entry : map) {
            index = entry.getKey();
            Long value = entry.getValue();
            if(value == 0) return entry.getKey();
            for (String string : strings) {
                if(string.charAt(0) == (char)('A' + entry.getKey())) {
                    index = - 1;
                    break;
                }
            }
            if(index != -1) break;
        }
        return index;
    }



}

